package com.atguigu.activemq.transportTestNio;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


public class JmsConsumer {
    public static final String QUEUE_NAME="Transport-nio+auto";
    public static final String ACTIVEMQ_URL="tcp://124.70.84.192:61618";
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        System.out.println("2号消费者");
        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if(null!=message&&message instanceof TextMessage){
                    //如果message不为空且message为TextMessage类的话执行下面内容
                    TextMessage textMessage= (TextMessage)message;
                    try {
                        String c01 = textMessage.getStringProperty("c01");
                        int vip = textMessage.getIntProperty("vip");
                        boolean vip8 = textMessage.getBooleanProperty("vip8");
                        System.out.println(textMessage.getText()+";"+c01+";"+vip+";"+vip8);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread.sleep(4000);
        messageConsumer.close();
        session.close();
        connection.close();
    }
}
